package utils;

import java.awt.Shape;
import java.rmi.RemoteException;

import javax.management.RuntimeErrorException;

import org.jhotdraw.draw.Figure;

import sr.logger.Logger;
import task.ChildTask;
import task.MasterTask;
import task.Task;
import util.FigureWrapper;

import master.ChildInterface;
import master.MasterInterface;
import network.MasterCommunicator;

public class TaskFactory 
{
	private static Logger logger = Logger.getLogger(TaskFactory.class);
	private static TaskFactory instance;
	private static MasterInterface master;
	private static ChildInterface child;
	public static void setUpFactory(MasterInterface nmaster)
	{
		master = nmaster;
	}
	public static void setChildInterface(ChildInterface childInterface)
	{
		child = childInterface;
	}
	public static Task createAddChatMessageTask(final String msg)
	{
		if(master == null)
			throw new RuntimeException("Master ref not set");
			return new MasterTask(master) {
			
			@Override
			public void run() {
				try {
					getMaster().addChatMessage(msg,logger.log("run","addChatMessage"));
				} catch (RemoteException e) {
					throw new RuntimeException("AddChatMessageTask");
				}
			}
		};
	}
	public static Task createAddRoomChatMessageTask(final String msg)
	{
		if(child == null)
			throw new RuntimeException("Child Server ref not set");
		return new ChildTask(child)
		{
			
			@Override
			public void run()
			{
				try
				{
					getChild().addMessage(msg,logger.log("run","addRoomChatMessage"));
				} catch (RemoteException e)
				{
					throw new RuntimeException("AddRoomChatMessageTask");
				}
			}
		};
	}
	public static Task createAddFigureTask(final Shape shape)
	{

		if(child == null)
			throw new RuntimeException("Child Server ref not set");
		return new ChildTask(child)
		{
			
			@Override
			public void run()
			{
				try
				{
					getChild().addFigure(shape,logger.log("run","addFigure"));					
				} catch (RemoteException e)
				{
					throw new RuntimeException("AddFigureTask");
				}
			}
		};
	}
	public static Task createOnStartButtonTask()
	{
		if(master == null)
			throw new RuntimeException("Master ref not set");
		return new MasterTask(master) {
			
			@Override
			public void run() {
				try {
					getMaster().onStartButton(Settings.getSettings().getMyId(),logger.log("run","onStartButton"));
				} catch (RemoteException e) {
					throw new RuntimeException("OnStartButtonTask");
				}
				
			}
		};
	}
}
